GetDayOfWeek Function

public function GetDayOfWeek(time) result(day)

Gets the day of week (0 - 6) (Sunday to Saturday The formula is:

(yearCode + monthCode + centuryCode + dateNumber - leapYearCode) mod 7

To calculate theyearCode, use this formula:

(YY + (YY div 4)) mod 7

YY is the last two digits of the year.

The monthCode:

  • January = 0
  • February = 3
  • March = 3
  • April = 6
  • May = 1
  • June = 4
  • July = 6
  • August = 2
  • September = 5
  • October = 0
  • November = 3
  • December = 5

The centuryCode for the Gregorian Calendar:

  • 1700s = 4
  • 1800s = 2
  • 1900s = 0
  • 2000s = 6
  • 2100s = 4
  • 2200s = 2
  • 2300s = 0

dateNumber is the day of Month leapYearCode: if the date is in a January or February of a leap year, you have to subtract one from your total before the final step.

References:

https://artofmemory.com/blog/how-to-calculate-the-day-of-the-week/

Arguments

Type IntentOptional Attributes Name
type(DateTime), intent(in) :: time

Return Value integer(kind=short)

returned value


Variables

Type Visibility Attributes Name Initial
integer(kind=short), public :: centuryCode

month code

integer(kind=short), public :: dateNumber

the day of month

integer(kind=short), public :: leapYearCode

leap year code

integer(kind=short), public :: month

month number

integer(kind=short), public :: monthCode

month code

character(len=4), public :: year
integer(kind=short), public :: yearCode

year code

integer(kind=short), public :: yy

the last two digits of the year

integer(kind=short), public :: yyyy

year (four digits)


Source Code

FUNCTION  GetDayOfWeek &
!
(time) &
!
RESULT (day)

IMPLICIT NONE

! Arguments with intent(in):
TYPE (DateTime), INTENT(IN) :: time

! Local variables:
INTEGER (KIND = short) :: day !! returned value
INTEGER (KIND = short) :: yearCode !! year code
INTEGER (KIND = short) :: monthCode !! month code
INTEGER (KIND = short) :: centuryCode !! month code
INTEGER (KIND = short) :: leapYearCode !! leap year code
INTEGER (KIND = short) :: dateNumber !! the day of month
INTEGER (KIND = short) :: yy !! the last two digits of the year
INTEGER (KIND = short) :: yyyy !! year (four digits)
INTEGER (KIND = short) :: month !! month number
CHARACTER (LEN = 4) :: year

!------------end of declaration------------------------------------------------

!compute the yearCode (YY + (YY div 4)) mod 7
yyyy = GetYear (time)
year = ToString (yyyy )
yy = StringToLong ( year (3:4) )
yearCode = MOD ( yy + INT(yy/4), 7)

!set the monthCode
month = GetMonth (time)
SELECT CASE (month )
   CASE (1,10)
       monthCode = 0
   CASE (2,3,11)
       monthCode = 3
   CASE (4,7)
       monthCode = 6
   CASE (5)
       monthCode = 1
   CASE (6)
       monthCode = 4
   CASE (8)
       monthCode = 2
   CASE (9,12)
       monthCode = 5
   END SELECT
   
!set the centuryCode
SELECT CASE ( yyyy )
    CASE (1700:1799)
        centuryCode = 4
    CASE (1800:1899)
        centuryCode = 2
    CASE (1900:1999)
        centuryCode = 0
    CASE (2000:2099)
        centuryCode = 6
    CASE (2100:2199)
        centuryCode = 4
    CASE (2200:2299)
        centuryCode = 2
    CASE (2300:2399)
        centuryCode = 0
    END SELECT

! set leapYearCode
leapYearCode = 0
IF ( IsLeapYear (yyyy) ) THEN
    IF ( month <= 2) THEN
        leapYearCode = -1
    END IF
END IF

!dateNumber
dateNumber = GetDay (time)

! yearCode + monthCode + centuryCode + dateNumber - leapYearCode) mod 7 
day = MOD (yearCode + monthCode + centuryCode + dateNumber - leapYearCode, 7)

RETURN
END FUNCTION GetDayOfWeek